import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_problem.dart';
import 'package:flutter_learn_vi/src/page/bloc/model_wrong_question.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';
import 'package:provider/provider.dart';

import 'data/count_model.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/13.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class TopicGapfillWidget extends StatefulWidget {
  final ProblemModel problemModel;
  final bool isShowError;

  TopicGapfillWidget(this.problemModel, this.isShowError);

  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TopicGapfillWidget> with WrongModel {
  FocusNode _focusNode = new FocusNode();

  @override
  void initState() {
    super.initState();
    _showAnswerMessage = checkQuestionFunction(widget.problemModel);
  }

  @override
  Widget build(BuildContext context) {
    if (widget.isShowError) {
      return Container();
    }
    return GestureDetector(
      onTap: () {
        _focusNode.unfocus();
      },
      child: Container(
          width: double.infinity,
          color: Colors.transparent,
          margin: EdgeInsets.all(12),
          child: Column(
            children: [
              _showAnswerMessage != null
                  ? Text("你输入的答案是：${widget.problemModel.themAnswerStr}")
                  : TextField(
                      readOnly: _showAnswerMessage != null,
                      focusNode: _focusNode,
                      maxLines: 2,
                      onChanged: (value) {
                        widget.problemModel.themAnswerStr = value;
                      },
                      decoration: InputDecoration(
                          hintText: "请输入答案", border: OutlineInputBorder()),
                    ),
              SizedBox(
                height: 20,
              ),
              _showAnswerMessage != null
                  ? buildErrorText()
                  : ElevatedButton(
                      child: Text("提交答案"),
                      onPressed: () {
                        if (widget.problemModel.themAnswerStr == null ||
                            widget.problemModel.themAnswerStr.trim().length ==
                                0) {
                          ToastUtils.showToast("请输入答案");
                          return;
                        }
                        checkRightAnswer();
                        setState(() {});
                      },
                    ),
            ],
          )),
    );
  }

  String _showAnswerMessage;

  void checkRightAnswer() {
    _showAnswerMessage = checkQuestionFunction(widget.problemModel);
    widget.problemModel.showAnswerMessage = _showAnswerMessage;
    context.read<CountModel>().check();
  }

  buildErrorText() {
    Color color = Colors.green;
    if (_showAnswerMessage.contains("回答错误")) {
      color = Colors.redAccent;
    }
    return Text(
      "${_showAnswerMessage.replaceAll(",", "")}",
      style: TextStyle(color: color),
    );
  }
}
